<template>
  <view class="">
    <!-- 搜索 -->
    <uni-search-bar @focus="toSearch" placeholder="请搜索"></uni-search-bar>
    <!-- 轮播 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(v, i) in swiperList" :key="i">
        <view class="swiper-item">
          <image :src="v" mode="" class="banner"></image>
        </view>
      </swiper-item>
    </swiper>
    <!-- 导航 -->
    <view class="menu-list">
      <view class="menu-item" v-for="(v, i) in menuList" :key="i" @click="handleClickMenuItem(v)">
        <image :src="v.url" mode=""></image>
        <text>{{v.name}}</text>
      </view>
    </view>
    <!-- 热门数据 -->
    <view class="hot">
      <view class="hot-top">
        <view class="title">
          {{hot.name}}
        </view>
        <uni-icons type="right"></uni-icons>
      </view>
      <my-hot-item v-for="(v, i) in hot.list" :key="i" :image="v.image" :title="v.title" :count="v.count"></my-hot-item>
    </view>
    <!-- tab数据 -->
    <view class="tab" v-if="tab.length !== 0">
      <scroll-view scroll-x :style="{ 'white-space': 'nowrap' }">
        <text class="title-item" :class="i === tabIndex ? 'active': ''" v-for="(v,i) in tab" @click="tabIndex = i" :key="i">
          {{ v.title }}
        </text>
      </scroll-view>
      <view class="tab-list">
        <my-hot-item v-for="(v, i) in tab[tabIndex].children" :key="i" :image="v.image" :title="v.title"
          :count="v.count">
        </my-hot-item>
      </view>
    </view>
  </view>

</template>

<script>
  export default {
    data() {
      return {
        swiperList: [],
        menuList: [],
        hot: {},
        tab: [],
        tabIndex: 0,
      }
    },
    onLoad() {
      this.getSwiperList()
      this.getHot()
      this.getMenuList()
      this.getTab()
    },
    methods: {
      toSearch() {
        uni.navigateTo({
          url: '/pages/search/search'
        })
      },
      handleClickMenuItem(v) {
        if (v.name === '更多') {
          // 进入全部页面
          uni.navigateTo({
            url: '/pages/list/list'
          })
        }
      },
      getSwiperList() {
        uni.request({
          url: 'http://localhost:3000/swiperList',
          success: (resp) => {
            this.swiperList = resp.data
          }
        })
      },
      getTab() {
        uni.request({
          url: 'http://localhost:3000/tab',
          success: (resp) => {
            this.tab = resp.data
          }
        })
      },
      getHot() {
        uni.request({
          url: 'http://localhost:3000/hotList',
          success: (resp) => {
            this.hot = resp.data
          }
        })
      },
      getMenuList() {
        uni.request({
          url: 'http://localhost:3000/menuList',
          success: (resp) => {
            this.menuList = resp.data
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .banner {
    width: 100%;
    height: 300rpx;
  }

  .menu-list {
    display: flex;
    flex-wrap: wrap;

    .menu-item {
      width: 20%;
      display: flex;
      flex-direction: column;
      align-items: center;

      image {
        width: 100%;
        padding: 10rpx;
        border-radius: 50%;
        height: 100rpx;
      }
    }
  }

  .hot-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 80rpx;
    padding: 0 20rpx;
  }

  .tab {
    .title-item {
      margin: 0 20rpx;
      height: 60rpx;
      line-height: 60rpx;

      &.active {
        color: red;
        border-bottom: 4rpx solid red;
      }
    }
  }
</style>
